<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\shop\goodscategory;
use DB;
use Cache;

class goodsCategoryController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'shop',
            '_goodsCategory' => 'am-active',
        ]);
    }

    //显示栏目首页
   public function index()
    {
        $categories=goodscategory::get_categories();
//        return $categories;exit;
        return view('admin.shop.goodsCategories.index')->with('categories',$categories);
    }


    //显示添加
    public function create()
    {
        $categories=goodscategory::get_categories();
//        return $categories;
        return view('admin.shop.goodsCategories.create')->with('categories',$categories);
    }

    //执行添加
    public function store(Request $request)
    {
        //        return $request->all();                  //相当于dump($_POST)
//        $article = new Article;
//        $article->title = $request->title;
//        $article->content = $request->input('content');
//        $article->save();
//        return $request->all();
        goodscategory::create($request->all()); //把所有提交上来的数据都存储到数据库中
        return redirect(route('shop.goodsCategory.index'))->with('category','新增栏目成功！');
    }

    //编辑分类
    function edit($id)
    {
        $categories=goodscategory::get_categories();
        $c=goodscategory::find($id);
        return view('admin.shop.goodsCategories.edit')->with('c',$c)->with('categories',$categories);

    }
    //更新分类
    function update(Request $request,$id)
    {
        $category=goodscategory::find($id);
        $category->update($request->all());//修改request上提交的所有数据
        return redirect(route('shop.goodsCategory.index'))->with('category','编辑成功！！！');
    }

    //删除
    function destroy($id)
    {
        goodscategory::destroy($id);
        return redirect(route('shop.goodsCategory.index'))->with('category','删除栏目成功！');
    }





    //是否显示
    public function is_show(Request $request)
    {
        $goodsCategory=goodscategory::find($request->id);
        $goodsCategory->is_show=!$goodsCategory->is_show;
        $goodsCategory->save();

    }

    //排序
    public function sort_order(Request $request)
    {
//        return $request->all();
        $category = goodscategory::find($request->id);
        $category->sort_order = $request->sort_order;
        $category->save();
    }



}
